{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ddc6a651",
   "metadata": {},
   "source": [
    "PySpark考试用的是2.4.5的版本,考试的时候选择Pyspark2.4.5版本就行，不用额外安装了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b5087167",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install pyspark==2.4.5"
   ]
  },
  {
   "cell_type": "raw",
   "id": "09543049",
   "metadata": {},
   "source": [
    "Spark中的数据类型：\n",
    "    RDD:数据没有结构，就是一个算子，默认情况下spark使用的数据都是RDD类型，当做数据挖掘的时候应该要把RDD转成spark的df类型方便操作\n",
    "    DataFrame:类似于二维表的，结构化数据类型\n",
    "    DataSet：不常用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0f9280ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os   \n",
    "os.environ['JAVA_HOME'] = \"D:/java/jdk-1.8\"   # 记得把地址改成自己的   在本地需要绑定java环境"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "269a95c3",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'pyspark.context'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpyspark\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcontext\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m SparkContext   \n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpyspark\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01msql\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m SparkSession\n\u001b[0;32m      3\u001b[0m sc \u001b[38;5;241m=\u001b[39m SparkContext(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlocal\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtest\u001b[39m\u001b[38;5;124m'\u001b[39m)   \u001b[38;5;66;03m#在本地启动\u001b[39;00m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pyspark.context'"
     ]
    }
   ],
   "source": [
    "from pyspark. import SparkContext   \n",
    "from pyspark.sql import SparkSession\n",
    "sc = SparkContext('local','test')   #在本地启动\n",
    "spark = SparkSession(sc)    # 建立连接\n",
    "# spark.stop() #停止spark环境\n",
    "sc  #本地环境没装java"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99722a98",
   "metadata": {},
   "outputs": [],
   "source": [
    "#吗的，本地环境不行，需要这需要那的，这个要到云上环境去练\n",
    "import pandas as pd\n",
    "# 从pandas dataframe创建spark dataframe\n",
    "names = ['tom','jack','lisan','wangwu','xiaoming','xiaohong']\n",
    "name_df = pd.DataFrame(data=names,columns=['name'])\n",
    "name_df['length'] = name_df['name'].apply(len)\n",
    "\n",
    "name_df = spark.createDataFrame(name_df) #把pandas的df转换成spark中的df\n",
    "name_df.show()    #展示"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
